﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using Infusion.Silverlight.Controls.Ribbon;
using GalaSoft.MvvmLight.Messaging;
using GalaSoft.MvvmLight.Command;
using ASPItalia.ModelVirtualCasting.Web.RiaServices;
using System.Collections;
using System.Reflection;
using System.Windows.Printing;
using System.Runtime.InteropServices.Automation;
using Uploader;

namespace ASPItalia.ModelVirtualCasting.Silverlight.BackOffice.ViewModels
{
    public class MainPageViewModel : ViewModelBase
    {
        public RelayCommand<object> LoadCommand { get; private set; }
        public RelayCommand<object> NewCommand { get; private set; }
        public RelayCommand<object> CopyCommand { get; private set; }
        public RelayCommand<object> PasteCommand { get; private set; }
        public RelayCommand<object> PrintCommand { get; private set; }
        public RelayCommand<object> ExportCommand { get; private set; }
        public RelayCommand<object> SaveCommand { get; private set; }
        public RelayCommand GoToCustomTypeCommand { get; private set; }
        public RelayCommand LoadDCVCommand { get; private set; }
        
        public MainPageViewModel()
        {
            Processing = new Tuple<bool, string>(false, string.Empty);

            LoadCommand = new RelayCommand<object>(Load, CanLoad);
            NewCommand = new RelayCommand<object>(New, CanNew);
            CopyCommand = new RelayCommand<object>(Copy, CanCopy);
            PasteCommand = new RelayCommand<object>(Paste, CanPaste);
            PrintCommand = new RelayCommand<object>(Print, CanPrint);
            ExportCommand = new RelayCommand<object>(Export, CanExport);
            SaveCommand = new RelayCommand<object>(Save, CanSave);
            GoToCustomTypeCommand = new RelayCommand(GoToCustomType, () => { return true; });
            LoadDCVCommand = new RelayCommand(() =>
            {
                Messenger.Default.Send<MessageType>(MessageType.DomainCollectionView);
            }, () => { return true; });
            
            Messenger.Default.Register<Tuple<bool, string>>(this, (m) => { Processing = m; });

            Messenger.Default.Register<IList>(this, (items) =>
            {
                Items.Clear();
                foreach (People item in items)
                    Items.Add(item);

                CopyCommand.RaiseCanExecuteChanged();
            });

            RibbonTab _rt1 = new RibbonTab()
            {
                Title = "Home",
                Groups =
                {
                    new RibbonTabGroup()
                    { 
                        Title="Risorse",
                        Items=
                        {
                           new RibbonButton(){ Text = "Carica",LargeImageSource = "Images/Reload.png",Command = LoadCommand}
                          ,new RibbonButton(){ Text = "Nuovo",LargeImageSource = "Images/New.png",Command = NewCommand}
                          ,new RibbonButton(){ Text = "Copia",LargeImageSource = "Images/Copy.png",Command = CopyCommand}
                          ,new RibbonButton(){ Text = "Incolla",LargeImageSource = "Images/Paste.png",Command = PasteCommand}
                          ,new RibbonButton(){ Text = "Stampa",LargeImageSource = "Images/Print.png",Command = PrintCommand}
                          ,new RibbonButton(){ Text = "Esporta",LargeImageSource = "Images/Export.png",Command = ExportCommand}
                          ,new RibbonButton(){ Text = "Salva",LargeImageSource = "Images/Save.png",Command = SaveCommand}
                        }
                    }
                }
            };
            _rt1.MouseLeftButtonDown += (obj, args) => { this.Frame.Navigate(new Uri("", UriKind.Relative)); };
            _menuItems.Add(_rt1);

            RibbonTab _rt2 = new RibbonTab()
            {
                Title = "CustomType",
                Groups =
                {
                    new RibbonTabGroup()
                    { 
                        Title="ICustomTypeProvider",
                        Items=
                        {
                           new RibbonButton(){ Text = "Carica",LargeImageSource = "Images/Reload.png",Command = GoToCustomTypeCommand}
                        }
                    }
                }
            };
            _rt2.MouseLeftButtonDown += (obj, args) => { this.Frame.Navigate(new Uri("/CustomTypePage", UriKind.Relative)); };
            _menuItems.Add(_rt2);

            RibbonTab _rt3 = new RibbonTab()
            {
                Title = "Linked Text",
                Groups =
                {
                    new RibbonTabGroup()
                    { 
                        Title="Linked Text",
                        Items=
                        {
                           new RibbonButton(){ Text = "Carica",LargeImageSource = "Images/Reload.png",Command = null}
                        }
                    }
                }
            };
            _rt3.MouseLeftButtonDown += (obj, args) => { this.Frame.Navigate(new Uri("/LinkedText", UriKind.Relative)); };
            _menuItems.Add(_rt3);

            RibbonTab _rt4 = new RibbonTab()
            {
                Title = "DomainCollectionView",
                Groups =
                {
                    new RibbonTabGroup()
                    { 
                        Title="DomainCollectionView",
                        Items=
                        {
                           new RibbonButton(){ Text = "Carica",LargeImageSource = "Images/Reload.png",Command = LoadDCVCommand}
                        }
                    }
                }
            };
            _rt4.MouseLeftButtonDown += (obj, args) => { this.Frame.Navigate(new Uri("/DomainCollectionView", UriKind.Relative)); };
            _menuItems.Add(_rt4);

        }

        private RibbonTabCollection _menuItems = new RibbonTabCollection();
        public RibbonTabCollection MenuItems
        {
            get { return _menuItems; }
            set
            {
                _menuItems = value;
                RaisePropertyChanged("MenuItems");
            }
        }

        private ObservableCollection<People> _items = new ObservableCollection<People>();
        public ObservableCollection<People> Items
        {
            get { return _items; }
            set
            {
                _items = value;
                RaisePropertyChanged("Items");
            }
        }

        Tuple<bool, string> _processing;
        public Tuple<bool, string> Processing
        {
            get { return _processing; }
            set
            {
                _processing = value;
                RaisePropertyChanged("Processing");
            }
        }

        public void Load(object parameter) { Messenger.Default.Send<MessageType>(MessageType.Load); }
        public bool CanLoad(object parameter) { return !Processing.Item1; }

        public void New(object parameter) { }
        public bool CanNew(object parameter) { return true; }

        public void Copy(object parameter)
        {
            string outData = string.Empty;

            foreach (People item in Items)
            {
                outData += ASPItalia.ModelVirtualCasting.Silverlight.BackOffice.Manager.Helper.ObjectToString(item);
            }

            Clipboard.SetText(outData);
        }

        public bool CanCopy(object parameter) { return Items.Count >= 1; }

        public void Paste(object parameter) { }
        public bool CanPaste(object parameter) { return true; }

        public void Print(object parameter) { Messenger.Default.Send<MessageType>(MessageType.Print); }
        public bool CanPrint(object parameter) { return true; }

        public void Export(object parameter) { Messenger.Default.Send<MessageType>(MessageType.Export); }
        public bool CanExport(object parameter)
        {
            return (AutomationFactory.IsAvailable && Application.Current.HasElevatedPermissions);
        }

        public void Save(object parameter) { Messenger.Default.Send<MessageType>(MessageType.Save); }
        public bool CanSave(object parameter)
        {
            return (AutomationFactory.IsAvailable && Application.Current.HasElevatedPermissions);
        }

        public void GoToCustomType()
        {
            Messenger.Default.Send<MessageType>(MessageType.LoadCustomType);
        }

        public Frame Frame { get; set; }
    }
}
